//
//  HNSH_BusinessCircleSearchViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/20.
//

#import "HNSH_BusinessCircleSearchViewController.h"

//view
#import "HNSH_BusinessCircleCell.h"

//vc
#import "HNSH_BusinessCircleDetailViewController.h"

@interface HNSH_BusinessCircleSearchViewController ()<UITextFieldDelegate>

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;
/* searchField */
@property (nonatomic, strong) UITextField *searchField;

@end

static NSString *HNSH_BusinessCircleCellID = @"HNSH_BusinessCircleCellID";

@implementation HNSH_BusinessCircleSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"" showLeftItem:YES];
    
    [self configNavView];
    
    [self configTableView];
}

- (void)configNavView {
    UIView *searchBgView = [[UIView alloc]init];
    searchBgView.backgroundColor = [UIColor colorWithHexString:@"#E3E6EB"];
    [self.view addSubview:searchBgView];
    searchBgView.layer.cornerRadius = 10.0f;
    searchBgView.layer.masksToBounds = YES;
    [searchBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.height.mas_equalTo(32);
        make.top.mas_equalTo(kStatusBarHeight+6);
        make.right.mas_equalTo(-20);
    }];
    
    UITextField *searchField = [[UITextField alloc]init];
    self.searchField = searchField;
    searchField.placeholder = @"搜索附近商家";
    searchField.textColor = [UIColor appBlackLabelColor];
    searchField.font = [UIFont systemFontOfSize:14];
    searchField.returnKeyType = UIReturnKeySearch;
    searchField.delegate = self;
    [searchBgView addSubview:searchField];
    [searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-10);
    }];
    
    UIImageView *searchIcon = [[UIImageView alloc]init];
    searchIcon.image = [UIImage imageNamed:@"base_nav_search"];
    [searchBgView addSubview:searchIcon];
    [searchIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(searchBgView.mas_centerY);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self headerRefreshing];
    return YES;
}

- (void)configTableView {
    self.tableView.rowHeight = 120;
    [self.tableView registerClass:[HNSH_BusinessCircleCell class] forCellReuseIdentifier:HNSH_BusinessCircleCellID];
    
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
}

- (void)refreshData:(NSInteger)page {
    NSDictionary *dict;
    NSString *codeString = @"";
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KCurrentLocation"]) {
        dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCurrentLocation"];
    }
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KSelectLocationCode"]) {
        codeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"KSelectLocationCode"];
    }
    if (![self.searchField.text isNotBlank]) {
        [self endRefresh:YES];
        [self.tableView reloadData];
        return;
    }
    [HNSH_ShopNetServer requestBusinessCircleShopListWithCategory_id:@"" keyword:self.searchField.text type:1 region_code:[codeString isNotBlank] ? codeString :dict[@"region_code"] latitude:[NSString stringWithFormat:@"%f",[dict[@"latitude"] doubleValue]] longitude:[NSString stringWithFormat:@"%f",[dict[@"longitude"] doubleValue]] page_num:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        [self endRefresh:YES];
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            NSArray *array = [NSArray yy_modelArrayWithClass:[HNSH_BusinessCircleListModel class] json:responseObject.resultset[@"list"]];
            [self.listAry addObjectsFromArray:array];
            [self.tableView reloadData];
            [self endRefresh:array.count>=10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_BusinessCircleCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_BusinessCircleCellID];
    cell.model = self.listAry[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_BusinessCircleDetailViewController *vc = [[HNSH_BusinessCircleDetailViewController alloc]init];
    vc.model = self.listAry[indexPath.section];
    [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
